// pages/user/user.js
var app = getApp();
var QRCode = require('../../utils/weapp-qrcode.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    type:"", 
    array: ['未知', '男', '女'],//性别
    index: 0,//性别
    user:"",//用户个人信息
    avatarUrl:"",//头像上传路径

    name:"",//修改个人昵称
    hobby:"",
    work:"",
    tel:"",
    hometown_province:"",//修改个人信息结束

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      type: options.type
    })
   
  
    var url = "https://fxapp.1024wl.cn";
   
    var qrcode = new QRCode('canvas', {

      text: url,
      width: 150,
      height: 150,
      colorDark: "#000000",
      colorLight: "#ffffff",
      correctLevel: QRCode.CorrectLevel.H,
    });
    this.people();//获取个人信息
    this.yongjin();
  },
  // 获取佣金列表
  yongjin:function(){
    var that = this;
    var authuid = wx.getStorageSync('uid');
    var authtoken = wx.getStorageSync('token');
    console.log('-uid--', authtoken)
    wx.request({
      url: app.globalData.baseUrl + "/a/sale/info",
      data: {
      },
      header: {
        authuid: authuid,
        authtoken: authtoken,
        'content-type': 'application/x-www-form-urlencoded',
        'TOKEN': "dxgANYXU4RqrAXo6OVwmBc9oodHWNXG2",

      },
      method: 'post',
      success(res) {
        console.log("---", res)
        // that.setData({
        //   user: res.data.result,
        // });
      }
    })
  },
  // 头像上传图片
  changeHead: function () {
    var that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ["original", "compressed"],
      sourceType: ["album", "camera"],
      success: function (e) {
        console.log('--1--',e);
        var a = e.tempFilePaths;
        var token = wx.getStorageSync('token');
        var authuid = wx.getStorageSync('uid');
        wx.uploadFile({
          // url: app.globalData.baseUrl + 'doctor/product/uploadFile',
          url: app.globalData.baseUrl + '/a/member/Member/avatar',
          filePath: a,
          name: "file",
          header: {
            'authuid': "authuid",
            'authtoken':'token',
            "Content-Type": "multipart/form-data",
            "file":"file"
          },
          formData: {
            
          },
          success: function (res) {
            console.log('上传结果:', res);
            var path = JSON.parse(res.data).data[0].filePath;
            that.setData({
              avatarUrls: path
            })
          },
          fail: function (e) {
            console.log(e)
          }
        })
        that.setData({
          avatarUrl: a
        })
        console.log('666', that.data.avatarUrl)
      }

    })
  },
  // 获取个人资料
  people:function(){
    // var userid = wx.getStorageSync('token');
    // console.log('--',userid);
    var that = this;
    wx.request({
      url: app.globalData.baseUrl + "/a/member/Member/info",
      data: {
      },
      header: {
        authuid: wx.getStorageSync('uid'),
        'content-type': 'application/x-www-form-urlencoded',
        'authtoken': wx.getStorageSync('token'),
        'TOKEN': "dxgANYXU4RqrAXo6OVwmBc9oodHWNXG2",

      },
      method: 'POST',
      success(res) {
        console.log("个人资料", res.data.result)
        that.setData({
          user: res.data.result,
        });
      }
    })
   
  },
  // 修改个人信息
  nicknameValue:function (e){
    // console.log("name", e.detail.value)
    this.setData({
      name: e.detail.value
    })
    
  },
  hobbyValue:function (e){
    // console.log("hobby", e.detail.value)
    this.setData({
      hobby: e.detail.value
    })
    
  },
  workValue:function (e){
    // console.log("work", e.detail.value)
    this.setData({
      work: e.detail.value
    })
    
  },
  telValue:function (e){
    
    this.setData({
      tel: e.detail.value
    })
    
  },
  hometown_province:function(e) {
    // console.log("hometown_province", e.detail.value)
    this.setData({
      hometown_province: e.detail.value
    })
    
  },
  // 提交保存
  addmydata:function (){
    var that = this;

    var nickname = that.data.name;
    var hobby = that.data.hobby;
    var hometown_province = that.data.hometown_province;
    var work = that.data.work;
    var tel = that.data.tel;
   

    console.log("name", that.data.name);
    console.log("hobby", that.data.hobby);
    console.log("work", that.data.work);
    console.log("tel", that.data.tel);
    console.log("hometown_province", that.data.hometown_province);

    wx.request({
      url: app.globalData.baseUrl + '/a/member/Member/update',
      data: {
        'nickname': nickname,
        'hobby': hobby,
        'work': work,
        'tel': tel,
        'hometown_province': hometown_province,
      },
      header: {
        
        'authuid': wx.getStorageSync('uid'),
        'authtoken': wx.getStorageSync('token'),
        'TOKEN': "dxgANYXU4RqrAXo6OVwmBc9oodHWNXG2",
        'content-type': 'application/json', 

        
      },
      method: 'POST',
      success(res) {
        console.log('完善个人信息:', res)
        // that.setData({
        //   'provincelist': res.data.data,
        // })
        if (res.data.status_code == 200) {
          wx.showToast({
            title: res.data.message,
            icon: 'success',
            duration: 2000
          })
          setTimeout(function () {
            wx.reLaunch({
              url: "../user-my/user-my"
            })
          }, 2000)
        }
      }
    })

  },
  // 修改个人信息结束



  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})